from pydantic import BaseModel, Field, field_validator
from datetime import date, datetime


class Emp(BaseModel):
    """
    员工的数据模型
    """

    id: int | None = Field(default=None, description="员工id")
    username: str = Field(description="用户名")
    name: str = Field(description="姓名")
    gender: int | None = Field(default=None, description="性别编号")
    image: str | None = Field(default=None, description="头像")
    job: int | None = Field(default=None, description="职位编号")
    entrydate: date | None = Field(default_factory=date.today, description="入职日期")
    deptId: int | None = Field(default=None, description="部门id")
    createTime: datetime | None = Field(
        default_factory=datetime.now, description="创建时间"
    )
    updateTime: datetime | None = Field(default=None, description="更新时间")

    @field_validator("entrydate", mode="before")
    def validate_date(cls, v):
        if isinstance(v, str):
            if v == "":
                return None
            return v.split("T")[0]
        return v
